package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.mapper.EchartsMapper;
import cn.kgc.ssm.service.EchartsService;
import cn.kgc.ssm.utils.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 课工场
 * @date 2024/3/11
 * @description
 */
@Service
public class EchartsServiceImpl implements EchartsService {

    @Resource
    private EchartsMapper echartsMapper;

    @Override
    public ResponseResult getPanelData() {

        //  1.最近一个月新会员数
         Integer memberNumber =  echartsMapper.selectNewMembers();
        // 2.最近一周的新活动数
        Integer activeNumber =  echartsMapper.selectNewActives();
        // 3.最近一周的新留言数
        Integer messageNumber =  echartsMapper.selectNewMessages();
        // 4.最近一个月的新岗位数
        Integer jobNumber =  echartsMapper.selectNewJobs();

        HashMap<String, Integer> panelData = new HashMap<>();
        panelData.put("member",memberNumber);
        panelData.put("active",activeNumber);
        panelData.put("message",messageNumber);
        panelData.put("job",jobNumber);


        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setData(panelData);   //  res.data.member
        return responseResult;
    }

    @Override
    public ResponseResult getLineData() {

        List<Map<String,Object>> lineData =  echartsMapper.getLineData();  // [1月 ,2月 ,3月 ,5月]
        System.out.println("lineData = " + lineData);
        List<Map<String,Object>>  result = new ArrayList<>();

        String o = (String) lineData.get(lineData.size() - 1).get("mont");
        String substring = o.substring(0, 1);
        System.out.println("substring = " + substring);
        Integer index = Integer.parseInt(substring);

        for (int i = 0; i < index; i++) {
            HashMap<String, Object> map = new HashMap<>();
            String month  = (i+1)+"月";
            Object  number = 0;
            for (Map<String, Object> lineDatum : lineData) {
                if (month.equals(lineDatum.get("mont"))){
                    number = lineDatum.get("number");
                }
            }
            map.put("mont",month);
            map.put("number",number);
            result.add(map);
        }
        System.out.println("result = " + result);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setData(result);
        return responseResult;
    }
}
